package com.guslegend.controller;


import com.guslegend.enums.BizCodeEnum;
import com.guslegend.enums.CouponCategoryEnum;
import com.guslegend.service.CouponService;
import com.guslegend.util.CommonUtil;
import com.guslegend.util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guslegend
 * @since 2025-05-30
 */
@RestController
@RequestMapping("/api/coupon/v1")
@Api(tags = "优惠券模块")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @ApiOperation("分页查询优惠券")
    @GetMapping("page_coupon_list")
    public JsonData pageCouponList(@ApiParam("查询第几页") @RequestParam(value = "page", defaultValue = "1") String page,
                                   @ApiParam("每页展示的条数") @RequestParam(value = "size", defaultValue = "1") String size){
        if (CommonUtil.parseInt(page)==-1||CommonUtil.parseInt(size)==-1){
            return JsonData.buildResult(BizCodeEnum.REQUEST_NUMBER_ERROR);
        }else {
            return JsonData.buildSuccess(couponService.pageCouponActivity(CommonUtil.parseInt(page),CommonUtil.parseInt(size)));
        }
    }

    @ApiOperation("领取优惠券")
    @GetMapping("add_coupon/{coupon_id}")
    public JsonData addCoupon(@ApiParam("优惠券id") @PathVariable("coupon_id") String couponId)throws InterruptedException{
        if (CommonUtil.parseLong(couponId)==-1){
            return JsonData.buildResult(BizCodeEnum.REQUEST_NUMBER_ERROR);
        }else {
            return couponService.addCoupon(CommonUtil.parseLong(couponId), CouponCategoryEnum.PROMOTION);
        }
    }
}

